/*
 * Created on Oct 22, 2003
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.vectrics.user;

import org.vectrics.common.db.JdbcTarget;
import org.vectrics.common.db.ProcessException;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.apache.log4j.Logger;
import java.text.SimpleDateFormat;



public class FindUsersJdbc implements JdbcTarget {
	private transient static Logger log = Logger.getLogger(FindUsersJdbc.class);
	private static SimpleDateFormat sqlDateFormat = new SimpleDateFormat("yyyy-MM-dd");
	
	private String queryStr = "";
	private Vector userSet = new Vector();		

	public FindUsersJdbc(PersonSearchCriteria criteria) {         
		       	
		log.debug("FindUsersJdbc() - constructing query");
		if (log.isDebugEnabled()) {
			log.debug("FindUsersJdbc() - criteria - lastName = " + criteria.getLastName());
			log.debug("FindUsersJdbc() - criteria - firstName = " + criteria.getFirstName());
		}
		
		String orderClause = " order by u.last_name, u.first_name, u.middle_name";
		String roleWhereClause = "";
		String lastNameWhereClause = "";
		String firstNameWhereClause = "";
		String userPrivilegesFrom = "";
		
		if ((criteria.getPrivileges() != null) && (criteria.getPrivileges().size() > 0)) {
			userPrivilegesFrom = "user_priv r,";			
			log.debug("criteria had role entries, creating where section for roles");
			roleWhereClause += " (u.user_name = r.user_name) and (";
			Iterator iter = criteria.getPrivileges().iterator();
			int index = 0;
			while (iter.hasNext()) {
				String roleName = (String)iter.next();
				if (index > 0)
					roleWhereClause += " or";
				roleWhereClause += " (r.role_name = '" + roleName + "')";
				index++;
			}
			roleWhereClause += ")";            
		}
		
		if ((criteria.getLastName() != null) && (criteria.getLastName().length() > 0)) {
			lastNameWhereClause = " (LOWER(u.last_name) like '" + criteria.getLastName() + "%')";
		}

		if ((criteria.getFirstName() != null) && (criteria.getFirstName().length() > 0)) {
			firstNameWhereClause = " (LOWER(u.first_name) like '" + criteria.getFirstName() + "%')";
		}

		String whereClause = ""; 
		if ((roleWhereClause.length() > 0) || (lastNameWhereClause.length() > 0) || (firstNameWhereClause.length() > 0)) {
			if (roleWhereClause.length() > 0) {
				if (whereClause.length() > 0)
					whereClause += " and ";
				whereClause += roleWhereClause;
			}
			if (lastNameWhereClause.length() > 0) {
				if (whereClause.length() > 0)
					whereClause += " and ";
				whereClause += lastNameWhereClause;
			}
			if (firstNameWhereClause.length() > 0) {
				if (whereClause.length() > 0)
					whereClause += " and ";
				whereClause += firstNameWhereClause;
			}
			whereClause = " where" + whereClause;
		}

        
		queryStr = "select distinct u.uid"
				+ " from " + userPrivilegesFrom + "user u" 
				+ whereClause 
				+ orderClause;

		if (log.isDebugEnabled()) {
			log.debug("FindUsersJdbc() - queryStr = " + queryStr);
		}
	}
	

	public List getUserSet() {
		return(userSet);
	}
	
	public void processResults(ResultSet results) throws ProcessException {
		log.debug("processResults() - called");
		int count = 0;
		try {
			while (results.next()) {
				String userId = results.getString("uid");
				userSet.add(userId);
				count++;
			}
			if (log.isDebugEnabled())
				log.debug("processResults() - count = " + count);
		}
		catch (java.sql.SQLException se) {
			log.error("processResults() - SQL Exception while processing results list", se);
			throw new ProcessException("processResults() - SQL Exception while processing results list:" + se.getMessage());
		}
	}

	
	public String getQuery() {
		return(queryStr);		
	}
}

